/**
 * 获取元素接口测试用例
 *
 * Created by yangyunze on 2021/01/04.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/analyse-files/cpp');

module.exports = describe('/GET file-elements/:id.json', function () {
  const administrator = new Administrator();
  let projectVersion = {};
  let otherElementId;
  const newUserIds = [];
  function url(elementId) {
    return `/file-elements/${elementId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject('c++');
      })
      .then((project) => project.createRandomVersion('c++'))
      .then((res) => {
        projectVersion = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常获取文件下元素
  it('it should GET element successfully', function (done) {
    projectVersion.getAllFunctions(benchmark)
      .then((allElements) => {
        const promises = allElements.map((element) => {
          if (!otherElementId) {
            otherElementId = element.functionId;
          }
          const { accessToken } = projectVersion.project.creator.token;
          return new Promise((resolve) => {
            chai.request(uri)
              .get(url(element.functionId))
              .set('Authorization', `Bearer ${accessToken}`)
              .end((err, res) => {
                res.should.have.status(200);
                common.should.be.functionMessage(res.body);
                resolve();
              });
          });
        });
        return Promise.all(promises);
      })
      .then(() => {
        done();
      })
      .catch(done);
  });

  // 不可获取不存在的元素
  it('it should fail to get element when the element is not exists', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .get(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可获取他人项目的元素
  it('it should fail to get element when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        newUser = newTester;
        return newTester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .get(url(otherElementId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
